using System.Collections.ObjectModel;
using System.ComponentModel;
using WpfApp1.Models;
using WpfApp1.Services;

public class StudentDashboardViewModel : INotifyPropertyChanged
{
    private readonly IStudentService _studentService;
    private readonly ICourseService _courseService;
    private readonly ICourseSelectService _courseSelectService;
    private string _currentStudentNumber;

    public ObservableCollection<Course> Courses { get; } = new ObservableCollection<Course>();
    public ObservableCollection<StudentCourseDto> StudentCourses { get; } = new ObservableCollection<StudentCourseDto>();
    public Student CurrentStudent { get; private set; }

    public StudentDashboardViewModel(
        string studentNumber,
        IStudentService studentService, 
        ICourseService courseService, 
        ICourseSelectService courseSelectService)
    {
        _currentStudentNumber = studentNumber;
        _studentService = studentService;
        _courseService = courseService;
        _courseSelectService = courseSelectService;
        
        LoadCurrentStudent();
        LoadAllCourses();
        LoadStudentCourses();
    }

    // 加载当前学生
    public void LoadCurrentStudent()
    {
        CurrentStudent = _studentService.GetStudentByNumber(_currentStudentNumber);
        OnPropertyChanged(nameof(CurrentStudent));
    }

    // 更新学生信息
    public void UpdateStudentInfo(Student student)
    {
        _studentService.UpdateStudent(student);
        LoadCurrentStudent();
    }

    // 加载所有课程
    public void LoadAllCourses()
    {
        Courses.Clear();
        var courses = _courseService.GetCourses();
        foreach (var course in courses)
        {
            Courses.Add(course);
        }
    }

    // 搜索课程
    public void SearchCourse(string courseNumber)
    {
        Courses.Clear();
        var course = _courseService.GetCourseByNumber(courseNumber);
        if (course != null)
        {
            Courses.Add(course);
        }
    }

    // 加载学生的选课
    public void LoadStudentCourses()
    {
        StudentCourses.Clear();
        var courseSelection = new CourseSelection(_currentStudentNumber, string.Empty, 0);
        var courses = _courseSelectService.GetStudentCoursesByStudentNumber(courseSelection);
        foreach (var course in courses)
        {
            StudentCourses.Add(new StudentCourseDto(
                course.CourseNumber,
                course.StudentNumber,
                course.CourseName,
                course.Credits,
                course.Hours,
                course.TeacherName,
                course.Grades
            ));
        }
    }

    // 选课
    public void SelectCourse(string courseNumber)
    {
        _courseSelectService.SelectCourses(courseNumber, _currentStudentNumber);
        LoadStudentCourses();
    }

    // 通知属性更改
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}